package com.android.questions.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;

import com.android.base.util.DisplayUtil;
import com.android.questions.R;
import com.android.questions.databinding.DialogSwitchProgressBinding;

/**
 * FileName: DialogSwitchProgress
 * Author: admin
 * Date: 2022/1/19 17:34
 * Description:
 */
public class DialogSwitchProgress extends Dialog {

    private DialogSwitchProgressBinding progressBinding;
    private final Activity activity;
    private int index = 0;
    private OnDismissLister dismissLister;

    public DialogSwitchProgress(Activity act) {
        super(act, R.style.dialog_loading);
        this.activity = act;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        progressBinding = DialogSwitchProgressBinding.inflate(activity.getLayoutInflater());
        setContentView(progressBinding.getRoot());

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        getWindow().setWindowAnimations(R.style.dialog_answer_correct);

        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                index++;
                progressBinding.answerNumberView.setProgress(index);
                if (index >= 100) {
                    dismiss();
                    if (dismissLister != null) {
                        dismissLister.onDismiss();
                    }
                    return;
                }
                new Handler().postDelayed(this, 20);
            }
        }, 20);
    }

    public DialogSwitchProgress setDismissLister(OnDismissLister lister) {
        this.dismissLister = lister;
        return this;
    }

    public interface OnDismissLister {
        void onDismiss();
    }
}